const express = require('express')
const router = express.Router()
const fs = require('fs')
const path = require("path")
const moment = require('moment')
const mysql = require('mysql')

let db = mysql.createPool({
    host: 'localhost',
    user: 'root',
    password: 'root',
    database: 'byy'
});

router.use((req, res, next) => {
    console.log("进入return_img", moment().format())
    next()
})

router.get('/img/:name', (req, res) => {
    res.sendFile(path.join(__dirname, '../page/upload', req.params.name))
})

router.get('/imgList', (req, res) => {
    let type = ""

    switch (req.query.index) {
        case "0":
            console.log("asd")
            type = "文字识别"
            break
        case "1":
            type = "植物识别"
            break
        case "2":
            type = "动物识别"
            break
        case "3":
            type = "LOGO识别"
            break
        case "4":
            type = "果蔬识别"
            break
        case "5":
            type = "地标识别"
            break
        case "6":
            type = "菜肴识别"
            break
        case "7":
            type = "通用识别"
            break
        case "8":
            type = "货币识别"
            break

    }
    console.log(req.query.username)
    db.query(`SELECT * FROM file where username='${req.query.username}' and type='${type}'`, (err, data) => {

        for (let i = 0; i < data.length; i++) {
            data[i].image_url = "http://111.229.43.251:8094/get/img/" + data[i].fileName
            data[i].article_type = 4
        }
        res.json(data)
    })
})

module.exports = router